<template>
  <n-button :loading="loading" :type="is_fave ? 'primary' :'tertiary'" strong secondary type="tertiary" size="tiny"
            @click="handleFava">
    收藏
  </n-button>
</template>
<script setup>
// 引入 naive-ui
import {NButton, createDiscreteApi} from "naive-ui";

// 声明参数
const props = defineProps({
  isfave: {
    type: Boolean,
    default: () => false
  },

  goods_id: {
    type: Number,
    default: () => 0
  },

  type: {
    type: String,
    default: () => "course"
  }
})

// 是否收藏
const is_fave = ref(props.isfave);

// 加载
const loading = ref(false);

// 点击收藏事件
const handleFava = () => {

  // 登陆之后才能收藏取消
  useHashAuth(async () => {
    loading.value = true;

    let data = {
      goods_id: props.goods_id,
      type: props.type
    }

    let {error} = is_fave.value ? await useUncollectApi(data) : await useCollectApi(data)

    loading.value = false;

    if (error.value) return;

    const {message} = createDiscreteApi(['message'])

    message.success((is_fave.value ? '取消收藏' : '收藏') + "成功")

    is_fave.value = !is_fave.value;

  })


}
</script>

<style scoped>

</style>